package atmGUI;

import util.Util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

//存款界面
public class WithdrawalPanel extends JPanel implements ActionListener {
    private Icon WithdrawalImage = Util.getIcon("/image/buttonSave.jpg");//存款按钮图
    private Icon backImage = Util.getIcon("/image/pane.jpg");//返回按钮图
    private ATM_GUI_Main frame;//顶级容器实例
    private List<User> usersList;//用户信息集合实例
    private JButton backBtn;//返回按钮
    private JLabel jLabelBack;//返回文本
    private JLabel withdrawalJLabel;//存款提示插件
    private JTextField iptWithdrawalMoney;//存款输入框
    private JButton WithdrawalBtn;//存款按钮

    public WithdrawalPanel(ATM_GUI_Main frame) {
        this.frame = frame;
        //设置空布局和透明度
        this.setLayout(null);
        this.setOpaque(false);
        //存款按钮
        WithdrawalBtn = new JButton(WithdrawalImage);
        WithdrawalBtn.setBounds(870, 220, 140, 50);
        this.add(WithdrawalBtn);
        WithdrawalBtn.addActionListener(this);
        //存款提示插件
        withdrawalJLabel = new JLabel("输入存款金额：");
        withdrawalJLabel.setBounds(230, 220, 400, 50);
        withdrawalJLabel.setFont(new Font("宋体", Font.PLAIN, 25));
        this.add(withdrawalJLabel);
        //存款输入框
        iptWithdrawalMoney = new JTextField();
        iptWithdrawalMoney.setBounds(420, 220, 300, 50);
        iptWithdrawalMoney.setFont(new Font("宋体", Font.BOLD, 35));
        this.add(iptWithdrawalMoney);

        //返回按钮
        jLabelBack = new JLabel("返  回");
        jLabelBack.setBounds(900, 300, 100, 50);
        jLabelBack.setForeground(Color.white);
        this.add(jLabelBack);
        jLabelBack.setFont(new Font("宋体", Font.BOLD, 25));
        backBtn = new JButton(backImage);
        backBtn.setBounds(870, 300, 140, 50);
        backBtn.setOpaque(false);
        this.add(backBtn);
        backBtn.addActionListener(this);//建立联系
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == backBtn) {
            frame.switchPanel("menu");
            iptWithdrawalMoney.setText("");
        } else if (e.getSource() == WithdrawalBtn) {
            double money = Double.parseDouble(iptWithdrawalMoney.getText());
            if (money % 100 != 0) {
                JOptionPane.showMessageDialog(this, "请输入100的倍数");
                return;
            }
            frame.getLoginUser().setBalance(frame.getLoginUser().getBalance() + money);
            JOptionPane.showMessageDialog(this, "存款成功，已存款：" + money);
            iptWithdrawalMoney.setText("");
        }
    }
}
